﻿<%@ WebHandler Language="C#" Class="GetFormulaTree" %>

using System;
using System.Web;
using System.Collections.Generic;
using System.Web.Script.Serialization;

public class GetFormulaTree : IHttpHandler
{

    private HsTec.Mobile.Green.BLL.ReportTemplateEntitysBLL bll = new HsTec.Mobile.Green.BLL.ReportTemplateEntitysBLL();

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string sId = context.Request.Form["id"];
        string sState = context.Request.Form["checkstate"];
        if (String.IsNullOrEmpty(sId))
        {
            TreeItem tdata = new TreeItem() { id = "0", checkstate = 0, complete = true, isexpand = true, showcheck = true, text = "自定义数据表", value = "", hasChildren = true };
            tdata.ChildNodes = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.ReportTemplateEntitys> listRoot = bll.GetEntityListByID(0);
            foreach (HsTec.Mobile.Green.Model.ReportTemplateEntitys rt in listRoot)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.RT_ID.ToString();
                ti0.value = rt.FieldTemplate == 0 ? "" : rt.FieldTemplateName + "!" + rt.FieldAlias;
                ti0.text = rt.FieldAlias;
                ti0.isexpand = false;
                ti0.hasChildren = rt.FieldChildCount > 0;
                ti0.complete = false;
                tdata.ChildNodes.Add(ti0);
            }
            //获取月度报表
            TreeItem ti = new TreeItem();
            ti.id = "0";
            ti.value = "tb_ReportAnalysis";
            ti.text = "月度报表";
            ti.isexpand = false;
            ti.hasChildren = true;
            ti.complete = true;
            ti.ChildNodes = new List<TreeItem>();
            tdata.ChildNodes.Add(ti);
            string mysql = " SELECT RTRIM(b.name) AS colname, CASE WHEN h.id IS NOT NULL " +
                               "       THEN 'PK' ELSE '' END AS primarykey, type_name(b.xusertype) " +
                               "       + CASE WHEN b.colstat & 1 = 1 THEN '[ID(' + CONVERT(varchar, ident_seed(a.name)) " +
                               "       + ',' + CONVERT(varchar, ident_incr(a.name)) + ')]' ELSE '' END AS type, b.length, " +
                               "       CASE b.isnullable WHEN 0 THEN 'N' ELSE 'Y' END AS isnull, ISNULL(e.text, '') " +
                               "       AS [default], ISNULL(c.[value], '') AS descript " +
                               " FROM (SELECT g.id, g.colid " +
                               "         FROM sysindexes f, sysindexkeys g " +
                               "         WHERE f.id = g.id AND f.indid = g.indid AND f.indid > 0 AND f.indid < 255 AND " +
                               "               (f.status & 2048) <> 0) h RIGHT OUTER JOIN " +
                               "       syscolumns b INNER JOIN " +
                               "       sysobjects a ON b.id = a.id LEFT OUTER JOIN " +
                               "       sys.extended_properties c ON b.id = c.major_id AND b.colid = c.minor_id LEFT OUTER JOIN " +
                               "       syscomments e ON b.cdefault = e.id ON h.id = b.id AND h.colid = b.colid " +
                               " WHERE (a.id = OBJECT_ID('tb_ReportAnalysis')) AND b.name like 'Powers%'" +
                               " ORDER BY b.colid";
            System.Data.DataView dvField = HsTec.Mobile.Green.Util.SQLHelper.ExecuteDataView(mysql);
            for (int i = 0; i < dvField.Count; i++)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = "0";
                ti0.value = "月度报表!" + dvField[i]["descript"].ToString().Replace("-", "");
                ti0.text = dvField[i]["descript"].ToString().Replace("-", "");
                ti0.isexpand = false;
                ti0.hasChildren = false;
                ti0.complete = true;
                ti.ChildNodes.Add(ti0);
            }
            //获取所有的中间计算项
            TreeItem ti2 = new TreeItem();
            ti2.id = "0";
            ti2.value = "";
            ti2.text = "中间公式项";
            ti2.isexpand = false;
            ti2.hasChildren = true;
            ti2.complete = true;
            ti2.ChildNodes = new List<TreeItem>();
            tdata.ChildNodes.Add(ti2);
            System.Data.DataView mydv = HsTec.Mobile.Green.Util.SQLHelper.ExecuteDataView("select IF_ID,IndexName  from tb_IndexFormula where FormulaParent='2'");
            for (int i = 0; i < mydv.Count; i++)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = mydv[i]["IF_ID"].ToString();
                ti0.value = "中间项!" + mydv[i]["IndexName"].ToString();
                ti0.text = mydv[i]["IndexName"].ToString();
                ti0.isexpand = true;
                ti0.hasChildren = false;
                ti0.complete = true;
                ti2.ChildNodes.Add(ti0);
            }
            //获取所有的指标项
            TreeItem ti3 = new TreeItem();
            ti3.id = "0";
            ti3.value = "";
            ti3.text = "指标项";
            ti3.isexpand = false;
            ti3.hasChildren = true;
            ti3.complete = true;
            ti3.ChildNodes = new List<TreeItem>();
            tdata.ChildNodes.Add(ti3);
            mydv = HsTec.Mobile.Green.Util.SQLHelper.ExecuteDataView("select IF_ID,IndexName  from tb_IndexFormula where FormulaParent='3'");
            for (int i = 0; i < mydv.Count; i++)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = mydv[i]["IF_ID"].ToString();
                ti0.value = "指标项!" + mydv[i]["IndexName"].ToString();
                ti0.text = mydv[i]["IndexName"].ToString();
                ti0.isexpand = true;
                ti0.hasChildren = false;
                ti0.complete = true;
                ti3.ChildNodes.Add(ti0);
            }
            context.Response.Write(tdata.ToJsonString());
        }
        else
        {
            List<TreeItem> list = new List<TreeItem>();
            List<HsTec.Mobile.Green.Model.ReportTemplateEntitys> listNode = bll.GetEntityListByID(int.Parse(sId));
            foreach (HsTec.Mobile.Green.Model.ReportTemplateEntitys rt in listNode)
            {
                TreeItem ti0 = new TreeItem();
                ti0.id = rt.RT_ID.ToString();
                ti0.value = rt.FieldTemplate == 0 ? "" : rt.FieldTemplateName + "!" + rt.FieldAlias;
                ti0.text = rt.FieldAlias;
                ti0.isexpand = false;
                ti0.hasChildren = rt.FieldChildCount > 0;
                ti0.complete = false;
                list.Add(ti0);
            }
            context.Response.Write(this.ListToJson(list));
        }
    }

    private string ListToJson(List<TreeItem> list)
    {
        JavaScriptSerializer jss = new JavaScriptSerializer();
        string sJson = String.Empty;
        foreach (TreeItem ti in list)
        {
            if (String.IsNullOrEmpty(sJson))
                sJson = jss.Serialize(ti);
            else
                sJson += "," + jss.Serialize(ti);
        }
        return "[" + sJson + "]";
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}